package com.liao.myapplication;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import androidx.annotation.Nullable;

public class MyContentProvider extends ContentProvider {
    private static final int SELECT_BOOK = 0;
    private static final int SELECT_USER = 1;
    private static final int INSERT_BOOK = 2;
    private static final int INSERT_USER = 3;
    private static final int DELETE_BOOK = 4;
    private static final int DELETE_USER = 5;
    private static final int UPDATE_BOOK = 6;
    private static final int UPDATE_USER = 7;
    private static UriMatcher matcher;
    private MyOpenHelper helper;

    static {
        matcher = new UriMatcher(UriMatcher.NO_MATCH);
        matcher.addURI("com.liao.myapplication", "select_book", SELECT_BOOK);
        matcher.addURI("com.liao.myapplication", "select_user", SELECT_USER);
        matcher.addURI("com.liao.myapplication", "insert_book", INSERT_BOOK);
        matcher.addURI("com.liao.myapplication", "insert_user", INSERT_USER);
        matcher.addURI("com.liao.myapplication", "delete_book", DELETE_BOOK);
        matcher.addURI("com.liao.myapplication", "delete_user", DELETE_USER);
        matcher.addURI("com.liao.myapplication", "update_book", UPDATE_BOOK);
        matcher.addURI("com.liao.myapplication", "update_user", UPDATE_USER);
    }


    public MyContentProvider() {
    }

    @Override
    public int delete(@Nullable Uri uri, String selection, String[] selectionArgs) {
        int match = matcher.match(uri);
        SQLiteDatabase db = helper.getWritableDatabase();
        int i = 0;
        if (match == DELETE_BOOK) {
            i = db.delete("book", selection, selectionArgs);
        } else if (match == DELETE_USER) {
            i = db.delete("user", selection, selectionArgs);
        }
        db.close();
        return i;
    }

    @Override
    public String getType(@Nullable Uri uri) {
        return null;
    }

    @Override
    public Uri insert(@Nullable Uri uri, ContentValues values) {
        int match = matcher.match(uri);
        SQLiteDatabase db = helper.getWritableDatabase();
        if (match == INSERT_BOOK) {
            db.insert("book", null, values);
        } else if (match == INSERT_USER) {
            db.insert("user", null, values);
        } else {
            throw new IllegalArgumentException("错误");
        }
        db.close();
        return uri;
    }

    @Override
    public boolean onCreate() {
        helper = new MyOpenHelper(getContext());
        return false;
    }

    @Override
    public Cursor query(@Nullable Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        int match = matcher.match(uri);
        System.out.println("MyContentProvider.query=====" + match);
        SQLiteDatabase db = helper.getReadableDatabase();
        switch (match) {
            case SELECT_BOOK:
                return db.query("book", projection, selection, selectionArgs, null, null, sortOrder);
            case SELECT_USER:
                return db.query("user", projection, selection, selectionArgs, null, null, sortOrder);
        }
        db.close();
        return null;
    }

    @Override
    public int update(@Nullable Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int match = matcher.match(uri);
        int i = 0;
        SQLiteDatabase db = helper.getWritableDatabase();
        if (match == UPDATE_BOOK) {
            i = db.update("book", values, selection, selectionArgs);
        } else if (match == UPDATE_USER) {
            i = db.update("user", values, selection, selectionArgs);
        }
        return i;
    }
}
